#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define fir first
#define sec second
#define chmin(a,b) (a=min(a,b))
#define chmax(a,b) (a=max(a,b))
int n,m,q,p[140010],lg2[270010],a[140010],ans[140010],cnt[270010][20],bl[270010],l[20],mn[270010];
bool d[530010];
int xn[4];
void pushup(int rt)
{
    if(!rt)return;
    bool c=d[rt];
    int ls=rt<<1,rs=rt<<1|1;
    if(!c)swap(ls,rs);
    int k=m-lg2[rt];
    bl[rt]=0;
    for(int i=0;i<=m+1;i++)cnt[rt][i]=0;
    for(int i=k;i<=m+1;i++)cnt[rt][i]=cnt[rs][i];
    bl[rt]=bl[rs];
    int flag=0;
    for(int i=0;i<k;i++)flag+=cnt[rs][i];
    if(mn[rs]<k)flag+=bl[rs];
    mn[rt]=max(mn[rs],k);
    if(flag)
    {
        for(int i=0;i<=m+1;i++)cnt[rt][i]+=cnt[ls][i];
        bl[rt]+=bl[ls];
        if(bl[ls])chmin(mn[rt],mn[ls]);
    }
    pushup(rt>>1);
}
void out()
{
    cout<<"out:"<<endl;
    for(int i=1;i<(1LL<<(m+1));i++)
    {
        cout<<" id="<<i<<endl<<" ";
        for(int j=0;j<=m+1;j++)cout<<" cnt["<<j<<"]="<<cnt[i][j];
        cout<<endl<<"  blank="<<bl[i]<<",mn="<<mn[i]<<endl;
    }
}
void solve()
{
    cin>>xn[0]>>xn[1]>>xn[2]>>xn[3];
    for(int i=(1LL<<(m+1))-1;i;i--)
    {
        bl[i]=(i>=(1LL<<m)?i-(1LL<<m)+1:bl[i<<1]+bl[i<<1|1]);
        mn[i]=0;
        memset(cnt[i],0,sizeof(cnt[i]));
    }
//    cout<<"m="<<m<<endl;
    for(int i=1;i<=n;i++)
    {
        int na=a[i]^xn[i&3];
        chmin(na,m+1);
  //      cout<<"   "<<i<<" "<<na<<endl;
        int u=(1LL<<m)+i-1;
        cnt[u][na]=i;
        bl[u]=0;
        pushup(u>>1);
 //       out();
        int id=1LL<<(m-lg2[i-1]-1);
        ans[i]=bl[id];
        for(int j=0;j<=m+1;j++)ans[i]+=cnt[id][j];
 //       cout<<"ans["<<i<<"]="<<ans[i]<<endl;
    }
    int pans=0;
    for(int i=1;i<=q;i++)pans^=(i*ans[p[i]]);
    cout<<pans<<endl;
}
string s;
signed main()
{
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    lg2[0]=-1;
    for(int i=2;i<262144;i++)lg2[i]=lg2[i/2]+1;
    cin>>n>>q;
    m=lg2[n-1]+1;
    for(int i=1;i<=n;i++)cin>>a[i];
    for(int i=1;i<=q;i++)cin>>p[i];
    for(int i=1;i<=m;i++)
    {
        l[i]=m-i;
        cin>>s;
        for(int j=0;j<(1LL<<(m-i));j++)d[(1LL<<(m-i))+j]=s[j]-'0';
    }
    l[0]=m;
    int t;
    cin>>t;
    while(t--)solve();
    return 0;
}
//17:50.
